package com.ncu702.backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.pagehelper.PageInfo;
import com.ncu702.backend.common.BaseResponse;
import com.ncu702.backend.common.ErrorCode;
import com.ncu702.backend.common.ResultUtils;
import com.ncu702.backend.model.dto.articles.ArticleAddRequest;
import com.ncu702.backend.model.dto.articles.ArticleUpdateRequest;
import com.ncu702.backend.model.dto.articles.ArticleVo;
import com.ncu702.backend.model.dto.comment.CommentAddRequest;
import com.ncu702.backend.model.dto.pages.PageH;
import com.ncu702.backend.model.entity.Article;
import com.ncu702.backend.model.entity.User;
import com.ncu702.backend.model.untils.TokeConfig;
import com.ncu702.backend.service.ArticleService;
import com.ncu702.backend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class ArticleController {
    @Resource
    private ArticleService articleService;
    @Resource
    private TokeConfig tokeConfig;
    @Resource
    private UserService userService;


    //根据articleId获取文章
    @GetMapping("/article")
    public BaseResponse<ArticleVo> getArticleById(@RequestParam(value = "articleId") String articleId) {
        ArticleVo article = articleService.getArticleById(articleId);
        //需要判断
        articleService.updateReadCount(articleId);
        return ResultUtils.success(article);
    }

    /*
    验证是否能修改文章
     */
    @GetMapping("/article/update1")
    public BaseResponse<ArticleVo> UserGetArticleById(
            @RequestParam(value = "articleId") String articleId,
            HttpServletRequest request
    ) {
        //获取jwt
        String jwtToken = request.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if (!checkToke) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, "token失效");
        }

        User tokeMessage = tokeConfig.getTokeMessage(jwtToken);
        Long uid = tokeMessage.getUid();
        String role = userService.UserByUid(uid).getUserRole();
        Long uuid = articleService.getArticleById(articleId).getUser().getUid();

        if (role.equals("admin") || (uid.equals(uuid) && role.equals("user"))) {
            ArticleVo article = articleService.getArticleById(articleId);
            return ResultUtils.success(article);
        } else {
            return ResultUtils.error(ErrorCode.FORBIDDEN_ERROR, "you do not have permission to modify!");
        }
    }


    /*
    真正修改文章
     */
    @PostMapping("/article/update2")
    public BaseResponse<Integer> update(
            @RequestBody ArticleUpdateRequest req,
            HttpServletRequest request) {
        //获取jwt
        String jwtToken = request.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if (!checkToke) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, "token失效");
        }
        User tokeMessage = tokeConfig.getTokeMessage(jwtToken);
        Long uid = tokeMessage.getUid();

        Integer integer = articleService.updateArticle(uid, req.getArticleId(), req.getTitle(), req.getLabel(),
                req.getSummary(), req.getContent(),req.getEditorType(),req.getMarkdownContent());
        return ResultUtils.success(integer);
    }


    /*
    根据用户id查找其所有文章
    */
    @GetMapping("/userall")
    public BaseResponse<List<ArticleVo>> getArticleByUid(@RequestParam long uid) {
        List<ArticleVo> article = articleService.getArticleByUid(uid);
        return ResultUtils.success(article);
    }


    //根据articleId删除文章
    @GetMapping("/article/delete")
    public BaseResponse<Integer> deleteArticleById(
            @RequestParam(value = "articleId") String articleId,
            HttpServletRequest request
    ) {
        //获取jwt
        String jwtToken = request.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if (!checkToke) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, "token失效");
        }
        Integer integer = articleService.deleteArticleById(articleId);
        return ResultUtils.success(integer);
    }


    //广场数据，默认是按照热度排序,传参是当前是第几页和每页是几条数据
    @GetMapping("/home")
    public BaseResponse<PageInfo<ArticleVo>> getAllArticleHome(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        PageInfo<ArticleVo> allArticleHome = articleService.getAllArticleHome(pageNum, pageSize);
        return ResultUtils.success(allArticleHome);
    }

    //广场数据，按照发布时间排序
    @GetMapping("/home/time")
    public BaseResponse<PageInfo<ArticleVo>> getAllArticleHomeToTime(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        PageInfo<ArticleVo> allArticleHomeToTime = articleService.getAllArticleHomeToTime(pageNum, pageSize);
        return ResultUtils.success(allArticleHomeToTime);
    }


    @GetMapping("/block")
    public BaseResponse<PageInfo<ArticleVo>> getAllArticleBlock(
            @RequestParam Integer op,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        PageInfo<ArticleVo> allArticleFront = articleService.getAllArticleBlock(op, pageNum, pageSize);
        return ResultUtils.success(allArticleFront);
    }


    @GetMapping("/block/time")
    public BaseResponse<PageInfo<ArticleVo>> getAllArticleBlockToTime(
            @RequestParam Integer op,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        PageInfo<ArticleVo> allArticleFront = articleService.getAllArticleBlockToTime(op, pageNum, pageSize);
        return ResultUtils.success(allArticleFront);
    }


    //精华，默认是按照热度排序,传参是当前是第几页和每页是几条数据
    @GetMapping("/elite")
    public BaseResponse<PageInfo<ArticleVo>> getAllArticleElite(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        PageInfo<ArticleVo> allArticleHome = articleService.getAllArticleElite(pageNum, pageSize);
        return ResultUtils.success(allArticleHome);
    }

    //精华，按照发布时间排序
    @GetMapping("/elite/time")
    public BaseResponse<PageInfo<ArticleVo>> getAllArticleEliteToTime(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        PageInfo<ArticleVo> allArticleHomeToTime = articleService.getAllArticleEliteToTime(pageNum, pageSize);
        return ResultUtils.success(allArticleHomeToTime);
    }


    /**
     * 发布新的文章
     */


    @PostMapping("/article/newPost")
    public BaseResponse<Integer> doNewPost(
            @RequestBody ArticleAddRequest req,
            HttpServletRequest request) {
        //获取jwt
        String jwtToken = request.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if (!checkToke) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, "token失效");
        }
        User tokeMessage = tokeConfig.getTokeMessage(jwtToken);
        Long uid = tokeMessage.getUid();
        String role = userService.UserByUid(uid).getUserRole();
        if (role.equals("ban")) {
            return ResultUtils.error(ErrorCode.FORBIDDEN_ERROR, "you are banned!");
        }


        Integer integer = articleService.addArticleByuid(uid, req.getTitle(), req.getLabel(), req.getSummary(),
                req.getEditorType(), req.getContent(), req.getMarkdownContent());
        return ResultUtils.success(integer);
    }

    /*
    设置为精华
     */
    @GetMapping("/article/addElite")
    public BaseResponse<Integer> addElite(
            @RequestParam("articleId") String articleId,
            HttpServletRequest request
    ) {
        //获取jwt
        String jwtToken = request.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if (!checkToke) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, "token失效");
        }
        Integer integer = articleService.addElite(articleId);
        return ResultUtils.success(integer);
    }


    /*
    取消为精华
     */
    @GetMapping("/article/deleteElite")
    public BaseResponse<Integer> deleteElite(
            @RequestParam("articleId") String articleId,
            HttpServletRequest request
    ) {
        //获取jwt
        String jwtToken = request.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if (!checkToke) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, "token失效");
        }
        Integer integer = articleService.deleteElite(articleId);
        return ResultUtils.success(integer);
    }


    /*
    设置为置顶
     */
    @GetMapping("/article/addTop")
    public BaseResponse<Integer> addTop(
            @RequestParam("articleId") String articleId,
            HttpServletRequest request
    ) {
        //获取jwt
        String jwtToken = request.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if (!checkToke) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, "token失效");
        }
        Integer integer = articleService.addTop(articleId);
        return ResultUtils.success(integer);
    }


    /*
    取消为置顶
     */
    @GetMapping("/article/deleteTop")
    public BaseResponse<Integer> deleteTop(
            @RequestParam("articleId") String articleId,
            HttpServletRequest request
    ) {
        //获取jwt
        String jwtToken = request.getHeader("jwtToken");
        //鉴权
        boolean checkToke = tokeConfig.checkToke(jwtToken);
        if (!checkToke) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, "token失效");
        }
        Integer integer = articleService.deleteTop(articleId);
        return ResultUtils.success(integer);
    }


    /**
     * 添加阅读量
     */
    @GetMapping("/article/readCount")
    public BaseResponse<Integer> addReadCount(@RequestParam("articleId") String articleId) {
        Integer integer = articleService.updateReadCount(articleId);
        return ResultUtils.success(integer);
    }


    /**
     * 获取文章作者的图片,暂时没有
     */
    @GetMapping("/article/info")
    public BaseResponse<User> getInfo(Integer uid) {
        User info = articleService.getInfo(uid);
        return ResultUtils.success(info);
    }


}
